.PHONY: all build run clean test deps help

BINARY_NAME=virt-manager-go
CLI_BINARY_NAME=vmctl
GO=go
GOFLAGS=-v
BUILD_DIR=bin

all: clean deps build

help:
	@echo "Virt-Manager Go - Build System"
	@echo ""
	@echo "Available targets:"
	@echo "  build      - Build the server binary"
	@echo "  run        - Run the server"
	@echo "  clean      - Clean build artifacts"
	@echo "  test       - Run tests"
	@echo "  deps       - Download dependencies"
	@echo "  fmt        - Format code"
	@echo "  lint       - Run linter"

deps:
	@echo "Downloading dependencies..."
	$(GO) mod download
	$(GO) mod tidy

build:
	@echo "Building $(BINARY_NAME)..."
	@mkdir -p $(BUILD_DIR)
	$(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) cmd/server/main.go
	$(GO) build $(GOFLAGS) -o $(BUILD_DIR)/$(CLI_BINARY_NAME) cmd/cli/main.go

run: build
	@echo "Starting $(BINARY_NAME)..."
	@./$(BUILD_DIR)/$(BINARY_NAME)

clean:
	@echo "Cleaning build artifacts..."
	@rm -rf $(BUILD_DIR)
	$(GO) clean

test:
	@echo "Running tests..."
	$(GO) test -v ./...

fmt:
	@echo "Formatting code..."
	$(GO) fmt ./...

lint:
	@echo "Running linter..."
	golangci-lint run

docker-build:
	@echo "Building Docker image..."
	docker build -t virt-manager-go:latest .

docker-run:
	@echo "Running Docker container..."
	docker run -d -p 8080:8080 \
		-v /var/run/libvirt/libvirt-sock:/var/run/libvirt/libvirt-sock \
		--name virt-manager-go \
		virt-manager-go:latest

swag:
	swag init -g cmd/server/main.go --exclude cmd/cli
	swag fmt --exclude cmd/cli